# -*- coding:utf-8 -*-

import random
import requests
from lxml import etree
import re

headers =  {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Connection':'keep-alive',
    'Host':'rms.jd.com',
    'Upgrade-Insecure-Requests':'1',
}

cookies = [
    '__jda=122270672.1489376377999682720988.1489376377.1489376377.1489804872.1; ipLoc-djd=1-72-2799-0; ipLocation=%u5317%u4EAC; ',
    "__jda=122270672.229977135.1465956774.1490071699.1490072897.43; __jdu=229977135; jdAddrId=; jdAddrName=;  ipLoc-djd=1-72-2799-0; ipLocation=%u5317%u4EAC;",
]

useragents = [
    'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
    'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
    'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Mobile Safari/537.36',
    'Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
    'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
    'Mozilla/5.0 (Linux; Android 4.3; Nexus 10 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
    'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263'
]

headers['User-Agent'] = random.choice(useragents)
headers['Cookie'] = random.choice(cookies)

class qrcode:
    
    qrcodeUrl = 'http://rms.jd.com/m/scan.do?p=de1615f38959949646641c34cc4d239acd9376ea878bf897'

    def __init__(self):
        # r = requests.get(self.qrcodeUrl,headers=headers,allow_redirects=False)
        # print r.history
        # if r.status_code == 302:
            # headers['Cookie'] = r.headers['Set-Cookie']
            # r = requests.get(r.headers['Location'],headers=headers,allow_redirects=False) 
            # print r.status_code
            # print r.headers['Location']
        
        
        headers = {
            'Host': 'union-click.jd.com',
            'User-Agent':'Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding': 'gzip, deflate, br',
            'Cookie': '__jda=122270672.229977135.1465956774.1490071699.1490072897.43; __jdu=45490826210489462; jdAddrId=; jdAddrName=; TrackID=1N0Uv6FmCWTM_6cO7zVjjATUROQnCjMhSNvgwGlpzlln0GHplbWPbXj6-ehSVQHkQj2A8Aq79ImyAqjyKcpUvRa2wh7ygUPys6mI9zUBXZfY; pinId=1ccx3qYP61hHcB5o6LlAxA; ipLoc-djd=1-72-2799-0; ipLocation=%u5317%u4EAC;',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
        }
        url = 'https://union-click.jd.com/jdc?e=&p=AyIHVCtaJQMiQwpDBUoyS0IQWhkeHAxBFVlETlcNVQtHRVdeQkQQWR5LQUYZDUQOQUsNRQxeB0gMU1cXWTVRR04LVQwYQUBMQxZJD3BKU1NZZzVlY3NpN2slaXIFRAFZBVFBQFMySwZRVh57O2s6dGxyYz1%2FNRYeFAFUEkcQBhIBSRxeHQUQEBRYBVJaTVUBYw4ZAhYQB0MeXXpHC1UYXhcVQFkRRB5WSmpSWRleFQsFRAlZOUxcUUInRQ5BDk1DCEZrXUZ3BgVCHhRnemFRYC1XQxJ0XHsZZQ4eN1QrWxQDEwZTGlwdByIESR1dFAsOAlEbXQkFFw9SGWsVBiJMOxprFDISAl0aWhMFEgdVH1wWMhIGVSuOnp%2FKtMLC1JoyEjdlGGs%3D&t=W1dCFFlQCxxURxcEAEAdQFkJBRlJUlFCF14FVlYMXwpOD1wdUF4QRwYbQ0JFF3UfUF4eB0JYD1dcVkQHTz5dQ0YLKXUrdGN8ZSVlJ2UVUVMXRR9WUEZgBUYfQQ5uaSV6Ontidm8xdVgJBBQGXAdeEQIUG1IeUxIABUYWRRxNXUBTLU5XFQYFVQ1eE21XHgdWHlkCUExDCl4YXXpHC1ceWxwVUVsXeQJLQVd1C04PGV1WWgg%3D'
        r=requests.get(url,headers=headers,allow_redirects=False)
        # print r.status_code
        
        url = re.search(r"https://union-click.jd.com/jda?[^;^']*", r.content,flags=0).group()
        
        r=requests.get(url,headers=headers,allow_redirects=False)
        
        print r.status_code
        print r.headers['Location']
        # f = open('jddesc.html','w+')
        # f.write(r.content)
        # f.close()
        
        


        
        
        
if __name__ == '__main__':
    qrcode()
